package com.web.kdl.controller.flyWheelV2;

import com.web.kdl.common.result.CommonResult;
import com.web.kdl.component.WebSocketService;
import com.web.kdl.pojo.Vo.DailyPowerAvgVo;
import com.web.kdl.pojo.Vo.FlyWheelData;
import com.web.kdl.pojo.Vo.FlywheelInfoVo;
import com.web.kdl.pojo.Vo.SiteAndWheelInfo;
import com.web.kdl.pojo.Vo.flywheel.FlywheelAlertInfoVo;
import com.web.kdl.pojo.Vo.flywheel.FlywheelMainInfoVo;
import com.web.kdl.pojo.Vo.flywheel.FlywheelVo;
import com.web.kdl.pojo.Vo.flywheel.FlyWheelEleCountVo;
import com.web.kdl.pojo.dto.FlywheelInfoDTo;
import com.web.kdl.pojo.dto.TableDto;
import com.web.kdl.pojo.form.FlywheelForm;
import com.web.kdl.service.createTable.factory.TableFactory;
import com.web.kdl.service.flywheel.FlyWheelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author： shulibin
 * @Date： 2025/7/24 8:58
 * @Describe：
 */
//  分表命名规则  wheel_alert_flywheel#{tableNum} wheel_data_flywheel#{tableNum} wheel_status_flywheel#{tableNum}
@RestController
//@RequestMapping("/flywheel")
public class FlyWheelController {
    @Autowired
    FlyWheelService flyWheelService;
    @Autowired
    private WebSocketService webSocketService;


//  统计飞轮最近7天的 日平均soc值 finsh
    @MessageMapping("/getFlywheelDayAverageSoc")
    @SendTo("/response/getFlywheelDayAverageSoc")
    public CommonResult getFlywheelDayAverageSoc(FlywheelForm form){
        List<Map<String, Object>> flywheelDayAverageSoc = flyWheelService.getFlywheelDayAverageSoc(form);

        if (flywheelDayAverageSoc == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelDayAverageSoc);
    }
//  统计 飞轮 机组充放电次数 最近 12个月 只取每月最后一天 最晚时间的值 若 本月没有数据 则取上个月的数据 若本月还未结束 则取当前最新数据
    @MessageMapping("/getFlywheelMonthCount")
    @SendTo("/response/getFlywheelMonthCount")
    public CommonResult getFlywheelMonthCount(FlywheelForm form){
        List<FlyWheelEleCountVo> flywheelMonthCount = flyWheelService.getFlywheelMonthCount(form);
        if (flywheelMonthCount == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelMonthCount);
    }

//    统计飞轮 日平均功率 finsh
    @MessageMapping("/getFlywheelDayAveragePower")
    @SendTo("/response/getFlywheelDayAveragePower")
    @PostMapping("/getFlywheelDayAveragePower")
    public CommonResult getFlywheelDayAveragePower(FlywheelForm form){
        List<DailyPowerAvgVo> flywheelDayAveragePower = flyWheelService.getFlywheelDayAveragePower(form);
        if (flywheelDayAveragePower == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelDayAveragePower);
    }

//  机组监控信息 近7天 finsh
    @PostMapping("/getFlywheelUnitInfo")
    @MessageMapping("/getFlywheelUnitInfo")
    @SendTo("/response/getFlywheelUnitInfo")
    public CommonResult getFlywheelUnitInfo(FlywheelForm form){
        List<FlywheelAlertInfoVo> flywheelUnitInfo = flyWheelService.getFlywheelUnitInfo(form);
        if (flywheelUnitInfo == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelUnitInfo);
    }


//  机组主要信息
    @MessageMapping("/getFlywheelMainInfo")
    @SendTo("/response/getFlywheelMainInfo")
    public CommonResult getFlywheelMainInfo(FlywheelForm form){
        FlywheelMainInfoVo flywheelMainInfo = flyWheelService.getFlywheelMainInfo(form);
        if (flywheelMainInfo == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelMainInfo);
    }
    // 创建飞轮相关数据表
    @PostMapping("/createTable")
    public void createTable(TableDto dto){
        flyWheelService.createTable(dto.getSiteName(),dto.getMatrixName(),dto.getFlywheelName());
    }
    // 获取飞轮最新数据
    @GetMapping("getLastValue")
    @MessageMapping("/getLastValue/{sumSite}/{matrixName}/{flyWheelName}")
    @SendTo("/response/getLastValue/{sumSite}/{matrixName}/{flyWheelName}")
    public CommonResult getLastValue(@Valid FlywheelInfoDTo flywheelInfoDTo){
        Map<String, FlyWheelData> flywheelVo =flyWheelService.getLastValue(flywheelInfoDTo);
        if (flywheelVo == null){
            return CommonResult.success("暂无数据");
        }
        return CommonResult.success(flywheelVo);
    }
}
